package me.yricky.alia.ui.gdsStructPage

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Button
import androidx.compose.material.Checkbox
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Window
import me.yricky.alia.ui.WindowInst
import me.yricky.alia.utils.gds.GDS
import me.yricky.alia.App
import me.yricky.alia.ui.AliaIcons
import me.yricky.alia.ui.component.ListWithScrollBar
import me.yricky.alia.ui.layoutPage.LayoutPageFactory
import me.yricky.alia.utils.gds.convert2Lib

/**
 * @author Yricky
 * @date 2022/1/29
 */
object GDSStructPageFactory {

    fun getWindowInst( path:String):WindowInst{
        return object :WindowInst(){
            val viewModel: GDSStructViewModel = GDSStructViewModel().also {
                it.loadFile(path)
            }

            override fun tag(): String =path

            @Composable
            override fun window() {
                Window(
                    title = path,
                    icon = AliaIcons.defaultWindowIcon(),
                    onCloseRequest = { App.removeWindow(this) }) {
                    Page(viewModel)
                }
            }

        }
    }

    @Composable
    fun Page(viewModel: GDSStructViewModel){
        val viewState by viewModel.viewState.collectAsState()
        if(viewState.isLoading){
            Box(Modifier.fillMaxSize()){
                CircularProgressIndicator(Modifier.align(Alignment.Center))
            }
        }else{
            Row {
                val showedRecord = viewState.records.filter { viewState.filter.contains(it.type) }
                Column(Modifier.weight(1f)) {
                    Row(Modifier.padding(4.dp,0.dp),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Button({
                            App.addWindow(LayoutPageFactory.getWindowInst(convert2Lib(viewState.records)))
                        }){
                            Text("open layout")
                        }
                        Spacer(Modifier.weight(1f))
                        Checkbox(
                            viewState.showFilter,
                            {viewModel.setShowFilter(it)}
                        )
                        Text("Filter(${showedRecord.size}/${viewState.records.size})")
                    }
                    ListWithScrollBar(Modifier.weight(1f)) {
                        items(showedRecord){ raw->
                            Column(
                                Modifier
                                    .fillMaxWidth()
                                    .clickable {  }
                                    .padding(4.dp)) {
                                Text("${raw.type}", fontFamily = FontFamily.Serif,fontSize = 24.sp)
                                GDS.Record.parse(raw).let{
                                    when(it){
                                        is GDS.Record.LabelRecord -> {}
                                        is GDS.Record.TimeRecord ->{
                                            Text("modifyTime:${it.modifyTime}")
                                            Text("accessTime:${it.accessTime}")
                                        }
                                        is GDS.Record.StringRecord ->{
                                            Text("name:${it.name}")
                                        }
                                        is GDS.Record.IdRecord -> {
                                            Text("id:${it.id}")
                                        }
                                        is GDS.Record.Presentation -> {
                                            Text("font:${it.getFont()}")
                                            Text("hAlign:${it.getHAlign()}")
                                            Text("vAlign:${it.getVAlign()}")
                                        }
                                        is GDS.Record.Units->{
                                            Text("Unit in user unit:${it.unitInUserUnit}")
                                            Text("Unit in meter:${it.unitInMeter}")
                                        }
                                        is GDS.Record.XY ->{
                                            Text("Data coord count:${(it.length-4)/8}")
                                            Text("Valid coord count:${it.shape.vertices.size}")
                                            Text("BBox:${it.shape.bBox}")
                                            if(it.shape.vertices.size == 1){
                                                Text("x:${it.shape.vertices[0].x},y:${it.shape.vertices[0].y}")
                                            }
                                        }
                                        is GDS.Record.STrans -> {
                                            Text("${it.reflection},${it.mag},${it.angle}")
                                        }
                                        is GDS.Record.Mag ->{
                                            Text("${it.double}")
                                        }
                                        is GDS.Record.Angle ->{
                                            Text("${it.double}")
                                        }
                                        else -> { Text("${it.length}") }
                                    }
                                }
                            }

                        }
                    }

                }

                AnimatedVisibility(
                    viewState.showFilter,
                    modifier = Modifier.weight(0.2f).background(Color(0xffcccccc))
                ){
                    ListWithScrollBar{
                        item {
                            Row(verticalAlignment = Alignment.CenterVertically) {
                                Checkbox(viewState.filter.isNotEmpty(),{ include->
                                    if (!include) {
                                        viewModel.clearFilter()
                                    } else {
                                        viewModel.resetFilter()
                                    }
                                })
                                Text("All")
                            }
                        }
                        items(GDS.RecordType.values()){
                            Row(verticalAlignment = Alignment.CenterVertically) {
                                Checkbox(viewState.filter.contains(it),{ include->
                                    if (!include) {
                                        viewModel.filterExclude(it)
                                    } else {
                                        viewModel.filterInclude(it)
                                    }
                                })
                                Text(it.name)
                            }
                        }
                    }
                }
            }
        }
    }
}
